package com.jstyle.jclifexd.daoManager;




import android.text.TextUtils;
import android.util.Log;

import com.jstyle.jclifexd.dao.GpsDataDao;
import com.jstyle.jclifexd.model.GpsData;
import com.jstyle.jclifexd.utils.DbManager;
import com.jstyle.jclifexd.utils.SharedPreferenceUtils;

import org.greenrobot.greendao.query.LazyList;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/10.
 */

public class GpsDataDaoManager {


    public static void insertPathRecord(GpsData gpsData){
        DbManager.getInstance().getDaoSession().getGpsDataDao().insert(gpsData);
    }
    public static void insertPathRecord(List<GpsData>pathRecordList){
        DbManager.getInstance().getDaoSession().getGpsDataDao().insertOrReplaceInTx(pathRecordList);
    }
//    public static GpsData getPathRecord(Long id){
//        GpsData pathRecordDao=DbManager.getInstance().getDaoSession().getGpsDataDao();
//        QueryBuilder<GpsData> builder = pathRecordDao.queryBuilder();
//        builder.where(GpsData.Properties.MId.eq(id));
//        return builder.unique();
//    }
    public static GpsData getPathRecord(String date){
        GpsDataDao pathRecordDao=DbManager.getInstance().getDaoSession().getGpsDataDao();
        QueryBuilder<GpsData> builder = pathRecordDao.queryBuilder();
        builder.where(GpsDataDao.Properties.Date.eq(date));
        return builder.unique();
    }
    public static List<GpsData> getPathRecord(String startDate, String endDate){
        List<GpsData> hrvDataList=new ArrayList<>();
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(TextUtils.isEmpty(address))return hrvDataList;
        QueryBuilder<GpsData> queryBuilder= DbManager.getInstance().getDaoSession().getGpsDataDao().queryBuilder();
        hrvDataList= queryBuilder.where(GpsDataDao.Properties.Address.eq(address),GpsDataDao.Properties.Date.between(startDate,endDate)).orderAsc(GpsDataDao.Properties.Date).list();
        return hrvDataList;
    }
    public static List<GpsData> getPathRecordData(String startDate, String endDate){
        List<GpsData> hrvDataList=new ArrayList<>();
        QueryBuilder<GpsData> queryBuilder= DbManager.getInstance().getDaoSession().getGpsDataDao().queryBuilder();
        hrvDataList= queryBuilder.where(GpsDataDao.Properties.Date.between(startDate,endDate)).orderAsc(GpsDataDao.Properties.Date).list();
        return hrvDataList;
    }
    public static List<GpsData> getAllPathRecord(){
        GpsDataDao pathRecordDao=DbManager.getInstance().getDaoSession().getGpsDataDao();
        QueryBuilder<GpsData> builder = pathRecordDao.queryBuilder().orderDesc(GpsDataDao.Properties.Date);
        return builder.list();
    }

    private static final String TAG = "GpsDataDaoManager";
    public static String getLastInsertDataTime() {
        String address= SharedPreferenceUtils.getSpString(SharedPreferenceUtils.KEY_ADDRESS);
        if(TextUtils.isEmpty(address))return "";
        QueryBuilder<GpsData> queryBuilder = DbManager.getInstance().getDaoSession().getGpsDataDao().queryBuilder();
        QueryBuilder<GpsData>queryGpsBuilder=queryBuilder.where(GpsDataDao.Properties.Address.eq(address));
        LazyList<GpsData> gpsDataList=queryGpsBuilder.listLazy();
        if(gpsDataList.size()==0){
            gpsDataList.close();
            return "";
        }
        gpsDataList=queryGpsBuilder.orderDesc(GpsDataDao.Properties.Date).listLazy();
        String lastInsertTime=gpsDataList.get(0).getDate();
        gpsDataList.close();
        return lastInsertTime;
    }

    public static void deleteAll(){
        DbManager.getInstance().getDaoSession().getGpsDataDao().deleteAll();;
    }
}
